package com.cx.bank.util;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.cx.bank.hibernateModel.Transactions;
import com.cx.bank.hibernateModel.Users;
import com.cx.bank.model.InfoBean;

public class TestClass {
	
	@Test
	public void f1() {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.openSession();
			tx = session.beginTransaction();

			Users users = new Users();
			users.setUsername("黄鹏");
			users.setPassword("123123");
			users.setMoney(100);
			
			session.save(users);
			tx.commit();
			
		}catch(Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	
	@Test
	public void f2() {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.openSession();
			tx = session.beginTransaction();

			Users users = (Users) session.load(Users.class, 1);
			System.out.println(users.getId()+users.getUsername());
			
			tx.commit();
			
		}catch(Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	
	@Test
	public void f3() {
		Session session = null;
		Transaction tx = null;
		Query query = null;
		Users users = null;
		String repsd=MD5.getMD5("1231231");
		try {
			session = HibernateUtils.openSession();
			tx = session.beginTransaction();
			String ss = "华s";
			String hql ="from Users u where u.username = ? and u.password = ?";
			
			query = session.createQuery(hql);
			query.setParameter(0, ss);
			query.setParameter(1, repsd);
			List<Users> userList=query.list();
			
			System.out.println(userList.isEmpty());
			
			for(Users s:userList) {
				System.out.println(s.getId()+s.getMoney()+s.getPassword()+s.getUsername());
			}
			
			tx.commit();
			
		}catch(Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	
	@Test
	public void f4() {
		Session session = null;
		Query query = null;
		InfoBean info = null;
		
		List<InfoBean> infoList = new ArrayList<InfoBean>();
		try {
			session = HibernateUtils.getCurrentSession();
			session.beginTransaction();
			
			String hql = "select user.username,transactions.transaction_details,transactions.transaction_time "
					+ "from Users user,Transactions transactions where users_id = (select id from Users where username = ?)";
			
			
			String hql2 = "from Transactions transactions "
					+ "where transactions.users.username = ?";
			
			query = session.createQuery(hql2);
			query.setParameter(0, "黄鹏");
			List<Transactions> userList = query.list();
			
			for(Transactions infos :userList) {
				Transactions transactions = infos;
				System.out.println(infos.getTransaction_details());
				System.out.println(infos.getTransaction_time());
				
				
				infoList.add(info);
			}
			System.out.println(infoList.size());
			
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	
	@Test
	public void f5() {
		
		Session session = null;
		Transaction tx = null;
		Query query = null;
		Users users = null;
		String repsd=MD5.getMD5("1231231");
		try {
			session = HibernateUtils.openSession();
			tx = session.beginTransaction();
			String ss = "华s";
			String hql = "select count(*) "
					+ "from Users user,Transactions transactions "
					+ "where users_id = (select id from Users where username = ?) and username = ?";
			query = session.createQuery(hql);
			query.setParameter(0, ss);
			query.setParameter(1, ss);

			Long count = (Long) query.uniqueResult();
			System.out.println(count);
			
		}catch(Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}
}
